package com.dreem.singleton.type7;

/**
 * @program: DesignParrtern
 * @description: 静态内部类
 * @author: Peng
 * @create: 2021-01-22  12:14
 **/
public class SingletonTest07 {
    public static void main(String[] args){
        System.out.println("静态内部类");
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println("instance1的hashcode() = " + instance1.hashCode());
        System.out.println("instance2的hashcode() = " + instance2.hashCode());
    }
}

// 静态内部类,推荐使用
class Singleton{
    private Singleton(){}

    private static class SingletonInstance{ // 静态内部类，类中有一个静态属性Singleton
        private final static Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance(){
        return SingletonInstance.INSTANCE;
    }
}
